<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="23">
            <el-form-item label="商户名称" prop="name"
                :rules="[
                  {required: true, message:'商户名称不能为空', trigger:'blur'},
                  { min: 1, message: '最小长度是1个字符', trigger: 'blur' }
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写商户名称"  maxlength="60"  minlength="1"   ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="23" v-for="language in languages" :key="language.id">
            <el-form-item :label="'商户名称'+language.label" :prop="'name'+language.value"
                :rules="[
                 ]">
              <el-input v-model="inputForm['name'+language.value]" :placeholder="'请填写商户名称'+language.label"     ></el-input>
           </el-form-item>
        </el-col>
       <el-col :span="23">
            <el-form-item label="铺位号" prop="room.id"
                :rules="[
                  {required: true, message:'铺位号不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择铺位号"
            labelName = 'name'
            labelValue = 'id'
            :value = "inputForm.room.id"
            :limit="1"
            @getValue='(value) => {inputForm.room.id=value}'
            :columns="[
            {
              prop: 'name',
              label: '铺位号'
            },
            {
              prop: 'floor.showName',
              label: '楼层'
            }
            ]"
            :searchs="[
            {
              prop: 'name',
              label: '铺位号'
            }
            ]"
            dataListUrl="/smis/floor/jfRoom/list"
            entityBeanName="jfRoom"
            queryEntityUrl="/smis/floor/jfRoom/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="23">
            <el-form-item label="展示铺位号" prop="roomShow"
                :rules="[
                  {required: true, message:'展示铺位号不能为空', trigger:'blur'}
                ]">
              <el-input v-model="inputForm.roomShow" placeholder="请填写展示铺位号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="23">
          <el-form-item label="商户状态" prop="status"
                        :rules="[
                 ]">
            <!--              <el-input v-model="inputForm.state" placeholder="请填写是否开启"     ></el-input>-->
            <el-radio-group v-model="inputForm.status">
              <el-radio v-for="item in this.$dictUtils.getDictList('merchants_status')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="23">
          <el-form-item label="热门推荐" prop="isRecommend"
                        :rules="[
                 ]">
            <!--              <el-input v-model="inputForm.state" placeholder="请填写是否开启"     ></el-input>-->
            <el-radio-group v-model="inputForm.isRecommend">
              <el-radio v-for="item in this.$dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
       <el-col :span="23">
            <el-form-item label="商户分类" prop="merchantsTypeIds"
                :rules="[
                 ]">
              <CasCader
                 style="width:100%"
                 url = "/smis/merchantstype/jfMerchantsType/treeData"
                :value="inputForm.merchantsTypeIds"
                :clearable="true"
                :accordion="true"
                @getValue="(value) => {inputForm.merchantsTypeIds=value}"/>
           </el-form-item>
        </el-col>

        <!-- <el-col :span="23">
            <el-form-item label="开始营业时间" prop="beginBusinessTime"
                :rules="[
                 ]">
              <el-time-select
                 v-model="inputForm.beginBusinessTime"
                 :picker-options="{
                   start: '00:30',
                   step: '00:15',
                   end: '23:45'
                 }"
                placeholder="选择时间">
              </el-time-select>
           </el-form-item>
        </el-col>
        <el-col :span="23">
            <el-form-item label="结束营业时间" prop="endBusinessTime"
                :rules="[
                 ]">
              <el-time-select
                 v-model="inputForm.endBusinessTime"
                 :picker-options="{
                   start: '00:30',
                   step: '00:15',
                   end: '23:45'
                 }"
                placeholder="选择时间">
              </el-time-select>
           </el-form-item>
        </el-col> -->
       <el-col :span="20" v-for="(item, index) in files" :key='index'>
             <el-form-item :label="item.label" :prop="item.value"
                 :rules="[
                  ]">
               <el-upload-ext
                 :limit = "1"
                 :accept = "item.format"
                 :fileType = "item.fileType"
                 :width = "item.width"
                 :height = "item.height"
                 v-model = "inputForm.file[item.value]"
               >
               </el-upload-ext>
            </el-form-item>
        </el-col>
        <el-col :span="23">
            <el-form-item label="商户介绍" prop="introduction"
                :rules="[
                 ]">
          <el-input type="textarea" :rows="4" v-model="inputForm.introduction" placeholder="请填写商户介绍"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="23" v-for="language in languages" :key="'introduction'+language.id">
            <el-form-item :label="'商户介绍'+language.label" :prop="'introduction'+language.value"
                :rules="[
                 ]">
              <el-input type="textarea" :rows="4" v-model="inputForm['introduction'+language.value]" :placeholder="'请填写商户介绍'+language.label"     ></el-input>
           </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import CasCader from '@/components/cascader'
  import ElUploadExt from '@/components/elUploadExt'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          name: '',
          name1: '',
          name2: '',
          name3: '',
          status: '',
          room: {
            id: ''
          },
          roomShow: '',
          merchantsTypeIds: '',
          merchantsTypeNames: '',
          beginBusinessTime: '',
          endBusinessTime: '',
          file: {
          },
          filesAll: '',
          isRecommend: '',
          introduction: '',
          introduction1: '',
          introduction2: '',
          introduction3: ''
        }
      }
    },
    components: {
      GridSelect,
      CasCader,
      ElUploadExt
    },
    computed: {
      languages: {
        get () {
          return this.$store.state.user.languages
        }
      },
      files: {
        get () {
          return this.$store.state.config.merchant
        }
      }
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建商户`
        } else if (method === 'edit') {
          this.title = '修改商户'
        } else if (method === 'view') {
          this.title = '查看商户'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          // if (this.files) {
          //   this.files.forEach((item) => {
          //     this.inputForm.file[item.keyStr] = ''
          //   })
          // }
          this.inputForm.status = '0'
          this.inputForm.isRecommend = '0'
          this.inputForm.file = {}
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/merchants/jfMerchants/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfMerchants)
              if(data.jfMerchants.filesAll && data.jfMerchants.filesAll.length > 0) {
                this.inputForm.file = JSON.parse(data.jfMerchants.filesAll)
              }
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.inputForm.filesAll = JSON.stringify(this.inputForm.file)
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/merchants/jfMerchants/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
